package com.hospital.vac.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hospital.vac.common.Result;
import com.hospital.vac.entity.Notification;
import com.hospital.vac.entity.Permission;
import com.hospital.vac.entity.User;
import com.hospital.vac.entity.VaccineOrder;
import com.hospital.vac.mapper.NotificationMapper;
import com.hospital.vac.mapper.PermissionMapper;
import com.hospital.vac.mapper.VaccineOrderMapper;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Date;
import java.util.TimeZone;
import java.util.UUID;

import static com.hospital.vac.constant.ConstantValue.BANNER;
import static com.hospital.vac.constant.ConstantValue.FAILED;

/**
 * @Author: ANLGIN
 * @CreateTime: 2023-03-10  14:01
 * @Description: TODO
 */
@RestController
@RequestMapping("/order")
public class VaccineOrderController {
    @Resource
    private VaccineOrderMapper vaccineOrderMapper;
    @Resource
    private NotificationMapper notificationMapper;

    /**
     * @return com.hospital.vac.common.Result<?>
     * @description 分页查询预约信息
     * @author ANLGIN
     * @since 2023/3/10 16:03
     */

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                              @RequestParam(defaultValue = "") Integer userId) {
        LambdaQueryWrapper<VaccineOrder> wrapper = Wrappers.lambdaQuery();
        Page<VaccineOrder> orderPage = null;
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(VaccineOrder::getOrderNo, search);
        } else {
            wrapper.like(VaccineOrder::getOrderNo, "");
        }
        if (userId == 1) {
            orderPage = vaccineOrderMapper.queryPages(new Page<>(pageNum, pageSize), wrapper);
        } else {
            orderPage = vaccineOrderMapper.queryPage(new Page<>(pageNum, pageSize), wrapper, userId);
        }

        return Result.success(orderPage);
    }

    @PostMapping
    public Result<?> save(@RequestBody VaccineOrder vaccineOrder) {
        if (vaccineOrder.getPhone().length()!=11 ||
                vaccineOrder.getVaccinePlace().isEmpty() ||
                vaccineOrder.getOrderTime() == null) {
            return Result.error(FAILED, "信息不完整");
        }
        vaccineOrder.setOrderNo(UUID.randomUUID().toString().replace("-", ""));
        vaccineOrder.setIsDelete(0);
        vaccineOrderMapper.insert(vaccineOrder);
        Notification notification = new Notification();
        notification
                .setUserId(vaccineOrder.getUserId())
                .setTitle("订阅消息")
                .setMessage("恭喜您预定的" + vaccineOrder.getVaccinePlace() + "已经接收到消息，你可以在预定的时间到社区接种疫苗，详情请查看预约信息")
                .setBanner(BANNER)
                .setLink("")
                .setCreatedAt( new DateTime().toJdkDate());
        notificationMapper.insertNotification(notification);
        return Result.success();
    }

    @DeleteMapping("/{vid}")
    public Result<?> delete(@PathVariable("vid") Integer id) {
        LambdaQueryWrapper<VaccineOrder> queryWrapper = new LambdaQueryWrapper<>();
        if (id != null) {
            queryWrapper.eq(VaccineOrder::getVid, id);
            vaccineOrderMapper.delete(queryWrapper);
            return Result.success();
        }
        return Result.error(FAILED, "删除失败");
    }
}
